package com.tanhua.manager.controller;

import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.manager.service.DashboardService;
import com.tanhua.manager.vo.ManagementDistributionVo;
import com.tanhua.manager.vo.ManagementSummaryVo;
import com.tanhua.manager.vo.ManagementUsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("management/dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 注册用户分布，行业top、年龄、性别、地区
     * @param sd 起始时间
     * @param ed 结束时间
     * @return
     */
    @GetMapping("distribution")
    @NoAuthorization
    public ResponseEntity<Object> dashboardDistribution(String sd,String ed){

        ManagementDistributionVo result = dashboardService.dashboardDistribution(sd, ed);
        if (result==null){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }

        return ResponseEntity.ok(result);
    }

    /**
     * 新增、活跃用户、次日留存率
     * @param sd
     * @param ed
     * @param type
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<Object> dashboardUsers(String sd,String ed,String type){
        ErrorResult errorResult = null;
        try {
            ManagementUsersVo result = dashboardService.dashboardUsers(sd, ed, type);
            if (result!=null){
                return ResponseEntity.ok(result);
            }
            errorResult = ErrorResult.builder().errCode("500").errMessage("时间范围不正确~~").build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 概要统计信息
     * @return
     */
    @GetMapping("summary")
    public ResponseEntity<Object> dashboardSummary(){
        ManagementSummaryVo result = dashboardService.dashboardSummary();
        if (result!=null) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
